<template>
	<view class="content" style="background-color: #F4F4F4;height: 100vh;">
		<tn-nav-bar bgClass="bg-gradual-blue">
			<view @click="showPasswordFun('NAME')">
				{{ title }}
			</view>
		</tn-nav-bar>
		<view :style="{paddingTop: vuex_custom_bar_height + 'px'}">

			<view class="margin tn-flex tn-flex-row-center">
				<zu-battery  :battery="readValue.RSOC" height='120rpx' width="300rpx" showPercent
					@click="showPasswordFun('CONFIG')"></zu-battery>
			</view>


			<view class="margin-top-lg">
				<tn-grid col="2" align="center">
					<tn-grid-item backgroundColor='#F4F4F4'>
						<view class="task-item b-card ">
							<view class="b-flex-space">
								<view class="task-item-describe">
									<view class="title b-fontw">
										电压
									</view>
									<view class="text">
										{{ readValue.Voltage }}
										<text class="text-lg" style="padding-left: 6rpx;">V</text>
									</view>
								</view>
								<image class="task-item-icon" src="/static/img/ble/dy.png" mode="">
								</image>
							</view>
						</view>
					</tn-grid-item>

					<tn-grid-item backgroundColor='#F4F4F4'>
						<view class="task-item b-card ">
							<view class="b-flex-space">
								<view class="task-item-describe">
									<view class="title b-fontw">
										电流
									</view>
									<view class="text">
										{{ readValue.CurCadc }}
										<text class="text-lg" style="padding-left: 6rpx;">A</text>
									</view>
								</view>
								<image class="task-item-icon" src="/static/img/ble/dl.png" mode="">
								</image>
							</view>
						</view>
					</tn-grid-item>
					<tn-grid-item backgroundColor='#F4F4F4'>
						<view class="task-item b-card ">
							<view class="b-flex-space">
								<view class="task-item-describe">
									<view class="title b-fontw">
										环境温度
									</view>
									<view class="text">
										{{ readValue.Temp }}
										<text class="text-lg" style="padding-left: 6rpx;">℃</text>
									</view>
								</view>
								<image class="task-item-icon" src="/static/img/ble/wd.png" mode="">
								</image>
							</view>
						</view>
					</tn-grid-item>
					<tn-grid-item backgroundColor='#F4F4F4'>
						<view class="task-item b-card ">
							<view class="b-flex-space">
								<view class="task-item-describe">
									<view class="title b-fontw">
										MOS温度
									</view>
									<view class="text">
										{{ readValue.Tmos }}
										<text class="text-lg" style="padding-left: 6rpx;">℃</text>
									</view>
								</view>
								<image class="task-item-icon" src="/static/img/ble/wd.png" mode="">
								</image>
							</view>
						</view>
					</tn-grid-item>
				</tn-grid>
			</view>

			<view class="margin">
				<view class="tn-margin-top-sm">
					<tn-list-cell :radius="true">
						<view class="list-icon-text">
							<view class="list__left">
								<view class="list__left__text">
									<text class="text-lg text-bold padding-left-sm">充电开关状态</text>
								</view>
							</view>
							<view class="list__right">
								<tn-switch activeColor="#409eff" @change='e => setMos(e, "charge")'
									v-model="readValue['PackStatus'][2].value * 1 === 1 ? true : false"></tn-switch>
							</view>
						</view>
					</tn-list-cell>
				</view>

				<view class="tn-margin-top-sm">
					<tn-list-cell :radius="true">
						<view class="list-icon-text">
							<view class="list__left">
								<view class="list__left__text">
									<text class="text-lg text-bold padding-left-sm">放电开关状态</text>
								</view>
							</view>
							<view class="list__right">
								<tn-switch activeColor="#409eff" @change='e => setMos(e, "discharge")'
									v-model="readValue['PackStatus'][1].value * 1 === 1 ? true : false"></tn-switch>
							</view>
						</view>
					</tn-list-cell>
				</view>

			</view>

			<!-- 告警信息 -->
			<view class="margin">
				<view class="tn-flex tn-flex-row-left tn-flex-wrap">
					<block v-for="(item, index) in readValue.ALARM" v-if="item && item.value * 1 > 0 && item.desc[0]"
						:key='index'>
						<view
							class="justify-content-item tn-text-center tn-margin-xs tn-radius bg-flex-shadow tn-shadow-blur">
							<tn-tag width="180rpx" backgroundColor="#01BEFF"
								margin="10rpx 10rpx">{{item.desc[0]}}</tn-tag>
						</view>
					</block>
				</view>
			</view>

			<view class="margin bg-white shadow-lg radius-25">
				<demo-title title="电芯电压">
					<tn-grid col="4" align="around">
						<tn-grid-item v-for="(item,index) in readValue['VCell']" :key="index">
							<view style="width: 100%;display: flex; justify-content: center;">
								<view class="margin-tb-sm"
									:class='item*1===readValue["Vmax"]*1?"bg-img-dc-max":item*1===readValue["Vmin"]*1?"bg-img-dc-min":"bg-img-dc"'>
									<view
										style="display: flex; justify-content: space-around;align-items: center;flex-direction: column;width: 100%;height: 100%;padding: 5px;">
										<view class="text-bold">{{item+" "}}
											<text class="text-sm"> V</text>
										</view>
										<view class="text-sm">电芯 {{index+1}}</view>
									</view>
								</view>
							</view>
						</tn-grid-item>
					</tn-grid>
				</demo-title>
			</view>
		</view>


		<view style="height: 10vh;"></view>

		<!-- 修改蓝牙名称弹窗 -->
		<template v-if="showDialog">
			<tn-modal :value="showDialog" @click="reName" @cancel="onDialogClose" custom :button="buttons"
				padding='30rpx'>
				<view class="custom-modal-content">
					<tn-form :labelWidth="140" style="width: 100%;">
						<tn-form-item label="蓝牙名称: " :borderBottom="false">
							<tn-input v-model="dialogInput" border :placeholder="bleName"></tn-input>
						</tn-form-item>
					</tn-form>
				</view>
			</tn-modal>
		</template>

		<!-- 密码弹窗 -->
		<template v-if="showPassword">
			<tn-modal :value="showPassword" @click="e=>onDialogClick(e,'CHECK')" @cancel="onDialogClose" custom
				:button="buttons" padding='30rpx'>
				<view class="custom-modal-content">
					<tn-form :labelWidth="80" style="width: 100%;">
						<tn-form-item label="密码:" :borderBottom="false">
							<tn-input v-model="dialogInput" border></tn-input>
						</tn-form-item>
					</tn-form>
				</view>
			</tn-modal>
		</template>

	</view>
</template>

<script>
	import {
		saveAlertLog
	} from '@/api/modules/device.js'

	import utils from '@/common/utils/utils.js';
	import BLE from "@/libs/bluetooth/BLE.js";
	import Protocol from "@/libs/protocol/chaoliyuan/protocol.js";
	import BLE_DATA from "@/libs/protocol/chaoliyuan/bleData.js";

	import demoTitle from '../components/demo-title.vue'
	import zuBattery from '../components/zu-battery-cly/zu-battery.vue'

	const TAG = '0000000000000000000000000000000000000000'
	//查询所有参数指令
	const CMD = '010300000076C42C'

	let timer = null;
	let sendTimer = null;
	let isConnectTimer = null;


	export default {
		components: {
			demoTitle,
			zuBattery
		},
		data() {
			return {
				bleName: '',
				title: '',
				userLevels: [],
				dialogInput: undefined,
				showPassword: false,
				showDialog: false,
				dialogType: null,
				showPasswordCount: 0,
				isFirst: true,
				isWrite: false,
				sendEnable: false,
				readIndex: 0,
				readAllParams: BLE_DATA.allReadCmdArray,
				readValue: {},
				deviceInfo: {},
				sendCmd: CMD,
				resData: '',
				lenValue: 0,
				nValue: 0,
				xValue: 0,
				buttons: [{
						text: '取消',
						backgroundColor: '#AAAAAA',
						fontColor: '#FFFFFF'
					},
					{
						text: '确定',
						backgroundColor: '#01BEFF',
						fontColor: '#FFFFFF'
					}
				]
			}
		},
		onLoad(options) {
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: true
			});
			if (options.name) {
				this.bleName = options.name
				this.title = this.bleName
			}
			var that = this;
			let readValue = {}
			for (var i = 0; i < that.readAllParams.length; i++) {
				let t = that.readAllParams[i]
				if (t.type === 2) {
					readValue[t.name[0]] = ''
					readValue[t.name[1]] = ''
				} else {
					readValue[t.name] = ''
				}
			}
			this.readValue = JSON.parse(JSON.stringify(readValue))
			//设置值更改函数
			//that.SetValueChanged();
		},
		onHide() {
			if (sendTimer != null) {
				clearInterval(sendTimer)
				sendTimer = null;
			}
			if (timer != null) {
				clearInterval(timer)
				timer = null;
			}
			this.resData = ''
			this.readIndex = 0;
			this.endMqtt()
		},
		onShow() {
			let that = this
			that.SetValueChanged();
			if (sendTimer != null) {
				clearInterval(sendTimer)
				sendTimer = null;
			}
			if (timer != null) {
				clearInterval(timer)
				timer = null;
			}
			const state = getApp().globalData.bleObj.state
			if (state) {
				this.init()
			} else {
				console.log('开始连接蓝牙');
				BLE.Connect(getApp().globalData.bleObj.deviceId, () => {
					console.log('连接成功');
					this.init()
				})
			}
		},
		methods: {
			init() {
				let that = this
				that.isFirst = true
				that.isWrite = false
				that.sendFirst(this)
				timer = setInterval(() => {
					that.sendFirst(this)
					that.showPasswordCount = 0
				}, 2000)
				//定时发送数据到服务器
				sendTimer = setInterval(() => {
					that.sendDataToMqtt()
				}, 600)
				//开始连接MQTT
				const deviceId = that.bleName ? that.bleName : getApp().globalData.bleObj.name;
				// console.log('deviceId', deviceId);
				this.getMqttInfo(deviceId)
				// 获取用户等级密码信息
				this.$api.common.listUserLevel().then(res => {
					this.userLevels = {}
					if (res.code === 200) {
						for (var i = 0; i < res.data.length; i++) {
							let t = res.data[i]
							this.userLevels['level.' + t.configKey.split('level.')[1]] = t.configValue
						}
					}
					console.log('this.userLevels', this.userLevels);
				})
			},
			sendDataToMqtt() {
				let that = this
				var state = getApp().globalData.bleObj.state;
				if (that.sendEnable && state) {
					let messages = []
					for (let key in that.readValue) {
						let value = this.readValue[key];
						switch (key) {
							case 'State1':
							case 'State2':
							case 'State3':
							case 'ALARM':
							case 'MOSCtl':
							case 'HotCtl':
							case 'SettingState':
								value = [];
								let temp = this.readValue[key];
								for (var i = 0; i < temp.length; i++) {
									let t = temp[i]
									if (t.value > 0) {
										value.push(t.desc[0])
									}
								}
								break;
						}
						messages.push({
							id: key,
							value: Array.isArray(this.readValue[key]) ? value.join(',') : this.readValue[key],
							remark: ''
						})
					}
					that.$mqttTool.publish(
						`/${that.deviceInfo.productId}/${that.deviceInfo.serialNumber}/property/post`,
						JSON.stringify(messages), 'name')
				}
			},
			sendFirst(that) {
				if (!that) {
					that = this
				}
				if (that.isFirst && !that.isWrite) {
					that.isFirst = false
					that.resData = ''
					Protocol.sendCommand(that.sendCmd, () => {
						console.log('sendFirst');
					})
				}
			},
			//设置蓝牙请求数据
			SetValueChanged() {
				var that = this;
				//处理数据
				BLE.bleCallBack.CharacteristicValueChange = function(res) {
					//console.log(res);
					if (res != undefined && res != null && res != '') {
						if (res === TAG) {
							that.resData = ''
							that.isFirst = true
						} else {
							that.resData = that.resData + res.toUpperCase();
							if (that.resData.startsWith('0110') || res.startsWith('0110')) {
								console.log('that.resData', that.resData);
								that.isFirst = true
								that.isWrite = false;
								that.resData = ''
							} else if (that.resData.startsWith('0103')) {
								that.isFirst = true
								let crcStr = that.resData.substr(that.resData.length - 4, that.resData.length)
								let str = that.resData.substr(0, that.resData.length - 4)
								const crc = Protocol.crc16Modbus(str).toUpperCase();
								if (crc === crcStr) {
									if (that.sendCmd === CMD) {
										that.lenValue = that.resData.substr(4, 2)
										that.nValue = that.resData.substr(6, 4)
										that.xValue = that.resData.substr(10, 4)
										let result = that.resData;
										Protocol.solutionData(result, that.readAllParams, that.nValue, that.xValue,
											that.sendCmd)
										that.readAllParams.map(t => {
											if (t.type === 2) {
												if (t.value && t.value[1]) {
													that.readValue[t.name[0]] = t.value[0]
													that.readValue[t.name[1]] = t.value[1]
												} else {
													that.readValue[t.name[0]] = ''
													that.readValue[t.name[1]] = ''
												}
											} else {
												that.readValue[t.name] = t.value
											}

										})
										that.sendCmd = ''
										//console.log(that.resData);
										//console.log('that.readAllParams', that.readAllParams);
										//console.log('that.readValue', that.readValue);
										let startTen = parseInt('005C', 16) + parseInt(that.nValue, 16) + parseInt(that
											.xValue, 16)
										let tCmd = '0103' + startTen.toString(16).padStart(4, '0').toUpperCase() +
											'001F'
										that.sendCmd = tCmd + Protocol.crc16Modbus(tCmd)
										Protocol.sendCommand(that.sendCmd, () => {
											//console.log("发送------");
										})
									} else {
										let result = that.resData;
										Protocol.solutionData(result, that.readAllParams, that.nValue, that.xValue,
											that.sendCmd)
										that.readAllParams.map(t => {
											if (t.type === 2) {
												if (t.value && t.value[1]) {
													that.readValue[t.name[0]] = t.value[0]
													that.readValue[t.name[1]] = t.value[1]
												} else {
													that.readValue[t.name[0]] = ''
													that.readValue[t.name[1]] = ''
												}
											} else {
												that.readValue[t.name] = t.value
											}
										})
										that.sendCmd = CMD
										that.resData = ''
									}
									that.resData = ''
								}
							} else {
								that.resData = ''
								that.isFirst = true
							}
						}
					}
				}
			},
			getMqttInfo(deviceId) {
				let that = this;
				let productCode = getApp().globalData.bleVersion
				that.$api.device.getMqttInfo(deviceId, productCode).then(res => {
					if (!res.data || !res.data.device) {
						return
					} else {
						let device = res.data.device
						let mqttInfo = res.data.mqttInfo
						let info = {
							clientId: mqttInfo.clientId,
							username: mqttInfo.username,
							password: mqttInfo.passwd,
						}
						setTimeout(async () => {
							await this.connectMqtt(info)
							this.mqttSubscribe(device);
							this.mqttCallback();
						}, 100)
					}

				})
			},
			async mqttSendOk(messages) {
				let that = this
				that.$mqttTool.publish(
					`/${that.deviceInfo.productId}/${that.deviceInfo.serialNumber}/property/post`,
					JSON.stringify(messages), '')
			},
			/* 断开Mqtt消息服务器 */
			async endMqtt() {
				await this.$mqttTool.end();
				this.sendEnable = false
			},
			/* 连接Mqtt消息服务器 */
			async connectMqtt(info) {
				let that = this
				if (this.$mqttTool.client === null) {
					if (info && info.username) {
						await this.$mqttTool.connect(info);
						this.$mqttTool.client.on('connect', function(res) {});
					}
				}
			},
			/* Mqtt回调处理  */
			mqttCallback() {
				let that = this
				let topicArrs = [
					'/function/get',
					'/monitor/get'
				]
				this.$mqttTool.client.on('message', (topic, message, buffer) => {
					console.log('message:', message.toString());
					console.log('topic:', topic);
					let topics = topic.split('/');
					let str = '/' + topics[3] + '/' + topics[4];
					let productId = topics[1];
					let deviceNum = topics[2];
					that.deviceInfo = {
						productId: productId,
						serialNumber: deviceNum
					}
					message = JSON.parse(message.toString());
					if (!message) {
						return;
					};
					if (str === topicArrs[1]) {
						if (message.count * 1 > 0) {
							that.sendEnable = true
							setTimeout(() => {
								that.sendEnable = false
							}, message.count * 1000)
						} else {
							that.sendEnable = false
						}
						let name = getApp().globalData.bleObj.name
						utils.readListWithFilterExpire(name + "PROTECT").then(saveArr => {
							console.log('saveArr', saveArr);
							// 发送后台
							for (var i = 0; i < saveArr.length; i++) {
								let t = saveArr[i];
								t['productId'] = productId
								t['alertLevel'] = 3
								t['status'] = 1
								t['detail'] = ''
								saveAlertLog(t).then(res => {
									console.log('上传成功', res);
								})
							}
						})
					}
					// 收到 PC下发指令
					if (str === topicArrs[0]) {
						let allItems = that.readAllParams
						let allItemsMap = {};
						for (var i = 0; i < allItems.length; i++) {
							let t = allItems[i]
							if (t.type === 2) {
								allItemsMap[t.name[0]] = {
									...t,
									value: (t.value && t.value[0]) ? t.value[0] : ''
								}
								allItemsMap[t.name[1]] = {
									...t,
									value: (t.value && t.value[1]) ? t.value[1] : ''
								}
							} else {
								allItemsMap[t.name] = t
							}
						}
						console.log('allItemsMap', allItemsMap);
						for (var i = 0; i < message.length; i++) {
							let item = message[i]
							console.log(item);
							let params = allItemsMap[item.id]
							console.log('params', params);
							let v = parseFloat(item.value);
							if (!isNaN(v)) {
								console.log('v', v);
								let hex = ''
								if (params.type === 2) {
									let name0Val = allItemsMap[params.name[0]].value
									let name1Val = allItemsMap[params.name[1]].value
									if (item.id === params.name[0]) {
										name0Val = v
									} else {
										name1Val = v
									}
									switch (params.start) {
										case '004B':
											name0Val = name0Val - 30
											name1Val = name1Val - 30
											break;
										case '004C':
											name0Val = name0Val + 40
											name1Val = name1Val + 40
											break;
										case '004D':
											name0Val = name0Val - 30
											name1Val = name1Val - 30
											break;
										case '004E':
											name0Val = name0Val + 40
											name1Val = name1Val + 40
											break;
									}
									hex = name0Val.toString(16).padStart(2, '0').toUpperCase() + "" + name1Val
										.toString(16).padStart(2, '0').toUpperCase()
								}

								if (params.type === 0) {
									switch (params.start) {
										case '0054':
										case '0056':
										case '0061':
										case '0062': //总压过压保护、总压过放保护
											hex = (v * 100).toString(16).padStart(params.len * 2, '0')
											break;
										case '000E': //设置电池容量
											hex = (v * 1000).toString(16).padStart(8, '0') +
												(allItemsMap['FCC'].value * 1000).toString(16).padStart(8, '0') +
												(allItemsMap['RC'].value * 1000).toString(16).padStart(8, '0');
											params.len = 12
											break;
									}
								}
								console.log('hex', hex);
								let cmd = Protocol.buildWriteCmd(params, that.nValue, that.xValue, hex)
								that.isWrite = true;
								setTimeout(() => {
									that.isWrite = false;
									Protocol.sendCommand(cmd, async () => {
										await that.mqttSendOk([item])
									})
								}, 2000)
							}
						}
					}
				});
			},
			/** Mqtt订阅主题 */
			mqttSubscribe(device) {
				// 订阅当前设备状态和实时监测
				let topics = buildTopics(device);
				this.$mqttTool.subscribe(topics);

			},
			/** Mqtt取消订阅主题 */
			mqttUnSubscribe(device) {
				let topics = buildTopics(device);
				this.$mqttTool.unsubscribe(topics);
				console.log('取消订阅', topics);
				this.$mqttTool.end();
			},
			showDialogFun() {
				this.showDialog = true
			},
			/**
			 * 连续点击标题或者 电池容量
			 * @param {Object} flag
			 */
			showPasswordFun(flag) {
				let count = this.showPasswordCount++
				if (count > 2 && flag === 'NAME') {
					//弹窗修改蓝牙名称
					this.showPassword = true
					this.dialogType = 'NAME'
				}
				if (count > 5 && flag === 'CONFIG') {
					//弹窗修改参数
					this.showPassword = true
					this.dialogType = 'CONFIG'
				}
			},
			/**
			 *
			 */
			onDialogClose() {
				this.showDialog = false;
				this.dialogInput = null
			},
			reName(e, type) {
				let that = this
				if (e.index < 1) {
					that.showPassword = false;
					that.showDialog = false;
					that.dialogInput = ''
					return
				}
				let str = that.dialogInput
				let bmsCmd = 'EAD101'
				let hexString = that.$util.bin2Hex(str);
				let len = ((hexString.length / 2) + 4).toString(16).padStart(2, '0')
				let scendCmd = len + 'FA01' + hexString
				let crcHex = Protocol.crc8Modbus(scendCmd)
				let sendCmd = bmsCmd + scendCmd + crcHex + 'F5'
				Protocol.sendCommand(sendCmd.toUpperCase(), (res1) => {
					that.showDialog = false
					// 请求后台 修改后台数据
					that.$api.device.editName({
						oldName: that.bleName,
						newName: str
					}).then(res => {
						if (res.code === 200) {
							that.$tn.message.toast('修改成功')
							//console.log('sendCmd', sendCmd.toUpperCase());
							setTimeout(() => {
								BLE.Close()
								that.$util.pageBack()
							}, 2000)
						} else {
							that.$tn.message.toast('修改失败')
							bmsCmd = 'EAD101'
							hexString = that.$util.bin2Hex(that.bleName);
							len = ((hexString.length / 2) + 4).toString(16).padStart(2, '0')
							scendCmd = len + 'FA01' + hexString
							crcHex = Protocol.crc8Modbus(scendCmd)
							sendCmd = bmsCmd + scendCmd + crcHex + 'F5'
							Protocol.sendCommand(sendCmd.toUpperCase(), (res2) => {
							})
						}
					})
				})
			},
			/**
			 *
			 */
			onDialogClick(e, type) {
				let that = this
				let input = that.dialogInput
				console.log(that.dialogInput);
				if (e.index < 1) {
					that.showPassword = false;
					that.showDialog = false;
					that.dialogInput = ''
					return
				}
				if (type === 'CHECK') {
					that.showPassword = false
					// 判断密码
					if (that.dialogType === 'NAME' && input === that.userLevels['level.1']) {
						that.showDialog = true
						that.dialogInput = ''
						return
					}
					if (that.dialogType === 'CONFIG' && input === that.userLevels['level.2']) {
						that.dialogInput = ''
						uni.navigateTo({
							url: '/pages_ble/ble/cly/config?n=' + that.bleName
						})
					}
					that.showDialog = false

				} else {
					// 修改蓝牙名称
					if (that.dialogType === 'NAME') {
						let str = that.dialogInput
						let bmsCmd = 'EAD101'
						let hexString = that.$util.bin2Hex(str);
						let len = ((hexString.length / 2) + 4).toString(16).padStart(2, '0')
						let scendCmd = len + 'FA01' + hexString
						let crcHex = Protocol.crc8Modbus(scendCmd)
						let sendCmd = bmsCmd + scendCmd + crcHex + 'F5'
						Protocol.sendCommand(sendCmd.toUpperCase(), (res) => {
							that.showDialog = false
							setTimeout(() => {
								BLE.Close()
								that.$util.pageBack()
							}, 2000)
						})
					}
				}
			},
			setMos(e, name) {
				this.readValue['PackStatus'][name === 'discharge' ? 1 : 2].value = e.detail.value ? 1 : 0
				let val = Object.assign({}, {
					charge: this.readValue['PackStatus'][2].value * 1 === 1 ? true : false,
					discharge: this.readValue['PackStatus'][1].value * 1 === 1 ? true : false
				})
				let item = {
					start: '001B',
					isN: true,
					isX: true,
					len: 2,
					type: -1,
					name: 'MOSController',
					text: 'MOS控制',
				}
				console.log('val', val);
				let name2 = name === 'charge' ? name : 'discharge'
				let hex = '0003'
				if (val.charge === true && val.discharge === false) {
					hex = '0001'
				}
				if (val.charge === false && val.discharge === true) {
					hex = '0002'
				}
				if (val.charge === false && val.discharge === false) {
					hex = '0000'
				}
				let cmd = Protocol.buildWriteCmd(item, this.nValue, this.xValue, hex)
				Protocol.sendCommand(cmd, () => {
					console.log('cmddd', cmd);
				})
			},
		},
		beforeDestroy() {
			if (sendTimer != null) {
				clearInterval(sendTimer)
				sendTimer = null;
			}
			if (timer != null) {
				clearInterval(timer)
				timer = null;
			}
			if (getApp().globalData.bleObj.state) {
				BLE.Close()
			}

			this.resData = ''
			this.readIndex = 0;
			this.endMqtt()
		}
	}



	/**
	 * MQTT主题订阅配置
	 * @param {Object} device 设备信息
	 */
	function buildTopics(device) {
		let topics = [];
		let preUrl = '/' + device.productId + '/' + device.serialNumber;
		// 订阅当前设备状态和实时监测
		let functionGet = preUrl + '/function/get';
		let topicInfo = preUrl + '/info/get';
		let topicMonitor = preUrl + '/monitor/get';
		topics.push(functionGet);
		topics.push(topicInfo);
		topics.push(topicMonitor);
		return topics
	}

	function buildStateItems(key, items) {
		let arr = []
		if (key === 'VCell' || key === 'MOSCtl' || key === 'HotCtl') {
			return items
		}

		return arr;
	}
</script>


<style scoped lang="scss">
	.radius-25 {
		border-radius: 25rpx;
	}

	.b-card {
		margin: 26rpx 32rpx;
		padding: 25rpx;
		background-color: #FFFFFF;
		border-radius: 18rpx;
		box-shadow: 12rpx 12rpx 10rpx #a7a7a7;
	}

	.b-flex-space {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.b-fontw {
		font-weight: 600;
	}


	.task-item {

		.task-item-icon {
			width: 80rpx;
			height: 80rpx;
		}

		.task-item-describe {
			height: 100rpx;
			width: 150rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-right: 30rpx;

			.title {
				font-size: 24rpx;
			}

			.text {
				margin-top: 20rpx;
				font-size: 46rpx;
			}
		}

	}


	.bg-img-dc-max {
		background-image: url('/static/img/ble/dx-max.png');
		background-size: 100% 100%;
		color: #13227a;
		height: 90rpx;
		width: 86px;
	}

	.bg-img-dc-min {
		background-image: url('/static/img/ble/dx-min.png');
		background-size: 100% 100%;
		height: 90rpx;
		color: #0b988f;
		width: 86px;
	}

	.bg-img-dc {
		background-image: url('/static/img/ble/dx.png');
		background-size: 100% 100%;
		height: 90rpx;
		width: 86px;
		color: #1296db;
	}


	.list__options {
		display: flex;
		align-items: center;
		justify-content: space-evenly;
	}

	.list {
		&__left {
			display: flex;
			align-items: center;
			justify-content: flex-start;

			&__icon,
			&__image {
				margin-right: 18rpx;
			}
		}

		&__right {
			display: flex;
			align-items: center;
			justify-content: flex-end;
		}
	}

	.list-icon-text,
	.list-image-text {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.list-image-text {
		.list {
			&__left {
				&__image {
					width: 20rpx;
					height: 20rpx;
				}
			}
		}
	}


	.tn-modal-class {
		.custom-modal-content {
			width: 100%;
			height: 100%;
			margin-bottom: 20rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.icon {
				font-size: 100rpx;
				margin-bottom: 20rpx;
				color: $tn-main-color;
			}

			.text {
				font-size: 40rpx;
			}
		}
	}
</style>
